# _*_ coding:utf8 _*_
from django.shortcuts import render, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
import models
# Create your views here.

@login_required()
def Index(request):
    return render(request, 'base.html')


def acc_login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            return HttpResponseRedirect("/")
        else:
            login_err = '用户名或者密码错误！'
            return render(request, 'login.html', {'login_err':login_err})
    return render(request, 'login.html')


@login_required()
def acc_logout(request):
    logout(request)
    return HttpResponseRedirect("/")


def person(request):
    return render(request, 'person.html')


def host_list(request):
    selected_g_id = request.GET.get('selected_group')
    if selected_g_id:
        if selected_g_id.isdigit():
            selected_g_id = int(selected_g_id)
    return render(request, 'host/host_list.html', {'selected_g_id':selected_g_id})


def game_list(request):
    selected_c_id = request.GET.get('selected_cluster')
    if selected_c_id:
        if selected_c_id.isdigit():
            selected_c_id = int(selected_c_id)
    if selected_c_id:
        game_list = models.Games.objects.filter(cluster=selected_c_id)
    else:
        game_list = ""
    return render(request, 'game/game_list.html', {'selected_c_id':selected_c_id, 'game_list':game_list})


def game_add(request):
    pass
    return render(request, 'game/game_add.html')

